package cn.ffcs.uom.organization.component;

import lombok.Getter;
import lombok.Setter;

import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Treeitem;

import cn.ffcs.raptornuke.portal.SystemException;
import cn.ffcs.uom.common.key.ActionKeys;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.zkplus.zul.tree.node.impl.TreeNodeImpl;
import cn.ffcs.uom.organization.constants.OrganizationConstant;
import cn.ffcs.uom.organization.model.UnitedDirectory;

public class UnitedDirectoryTreeExt extends Div implements IdSpace {

	/**
	 * 
	 */
	private static final long serialVersionUID = 532521498062036747L;

	/**
	 * zul文件路径。
	 */
	private final String zul = "/pages/organization/united_directory_tree_ext.zul";
	/**
	 * 组织树
	 */
	private UnitedDirectoryTree unitedDirectoryTree;

	/**
	 * 选中的组织关系
	 */
	private UnitedDirectory unitedDirectory;

	/**
	 * 选中的组织TreeItem
	 */
	private Treeitem selectedTreeitem;

	/**
	 * 选择树类型
	 */
	@Getter
	@Setter
	private Listbox treeSelect;
	/**
	 * 增加根节点按钮
	 */
	@Getter
	@Setter
	private Toolbarbutton addRootButton;
	/**
	 * 增加孩子节点按钮
	 */
	@Getter
	@Setter
	private Toolbarbutton addChildButton;
	/**
	 * 删除节点按钮
	 */
	@Getter
	@Setter
	private Toolbarbutton delChildButton;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UnitedDirectoryTreeExt() {
		// 1. Create components (optional)
		Executions.createComponents(this.zul, this, null);
		// 2. Wire variables (optional)
		Components.wireVariables(this, this);
		// 3. Wire event listeners (optional)
		Components.addForwards(this, this, '$');
		/**
		 * 组织名称修改要改组织树节点名称
		 */
		UnitedDirectoryTreeExt.this.addForward(
				OrganizationConstant.ON_SAVE_UNITED_DIRECTORY_INFO,
				UnitedDirectoryTreeExt.this,
				"onSaveUnitedDirectoryInfoResponse");

		this.setButtonValid(false, false, false);
	}

	/**
	 * 增加根节点
	 * 
	 * @throws Exception
	 */
	// public void onAddRootNode() throws Exception {
	// if (!PlatformUtil.checkPermissionDialog(getPortletInfoProvider(),
	// ActionKeys.DATA_OPERATING)) {
	// return;
	// }
	// Treechildren treechildren = this.unitedDirectoryTree.getTreechildren();
	// if (treechildren != null && treechildren.getChildren() != null
	// && treechildren.getChildren().size() > 0) {
	// ZkUtil.showError("存在根节点,不能添加", "提示信息");
	// return;
	// }
	// String opType = "addUnitedDirectoryRootNode";
	// this.openAddNodeWindow(opType);
	// }

	/**
	 * 增加根节点
	 * 
	 * @throws Exception
	 */
	// private void onAddRootNodeResponse(UnitedDirectory unitedDirectory)
	// throws Exception {
	// Treechildren treechildren = this.unitedDirectoryTree.getTreechildren();
	// Treeitem treeitem = new Treeitem();
	// Treecell treecell = new Treecell("新增节点");
	// Treerow treerow = new Treerow();
	// treecell.setParent(treerow);
	// treerow.setParent(treeitem);
	// TreeNodeImpl treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
	// unitedDirectory);
	// treeitem.setValue(treeNodeImpl);
	// treechildren.appendChild(treeitem);
	// }

	/**
	 * 增加子节点
	 * 
	 * @throws Exception
	 */
	// public void onAddChildNode() throws Exception {
	// /**
	// * 操作数据权限
	// */
	// if (!PlatformUtil.checkPermissionDialog(getPortletInfoProvider(),
	// ActionKeys.DATA_OPERATING)) {
	// return;
	// }
	// if (this.unitedDirectoryTree.getSelectedItem() != null) {
	// String opType = "addUnitedDirectoryChildNode";
	// this.openAddNodeWindow(opType);
	// } else {
	// ZkUtil.showError("请选择节点", "提示信息");
	// }
	// }

	/**
	 * 增加子节点
	 * 
	 * @throws Exception
	 */
	// private void onAddChildNodeResponse(UnitedDirectory unitedDirectory)
	// throws Exception {
	// Treechildren treechildren = this.unitedDirectoryTree.getSelectedItem()
	// .getTreechildren();
	// // 没有下级
	// if (treechildren == null) {
	// /**
	// * 父节点设置下级孩子为null让其查库，避免增加了节点不展示的问题
	// */
	// TreeNodeImpl parentTreeNodeImpl = (TreeNodeImpl) this.unitedDirectoryTree
	// .getSelectedItem().getValue();
	// parentTreeNodeImpl.setChildren(null);
	// this.unitedDirectoryTree.getSelectedItem().setValue(
	// parentTreeNodeImpl);
	//
	// Treechildren tchild = new Treechildren();
	// Treeitem titem = new Treeitem();
	// Treerow trow = new Treerow();
	// Treecell tcell = new Treecell(unitedDirectory.getLabel());
	//
	// tcell.setParent(trow);
	// trow.setParent(titem);
	// TreeNodeImpl treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
	// unitedDirectory);
	// titem.setValue(treeNodeImpl);
	// titem.setParent(tchild);
	// tchild.setParent(this.unitedDirectoryTree.getSelectedItem());
	// } else {
	// // 已存在下级
	// Treeitem titem = new Treeitem();
	// Treerow trow = new Treerow();
	// Treecell tcell = new Treecell(unitedDirectory.getLabel());
	//
	// tcell.setParent(trow);
	// trow.setParent(titem);
	// TreeNodeImpl treeNodeImpl = new TreeNodeImpl<TreeNodeEntity>(
	// unitedDirectory);
	// titem.setValue(treeNodeImpl);
	// titem.setParent(treechildren);
	// }
	// }

	/**
	 * 打开界面
	 * 
	 * @param opType
	 */
	// private void openAddNodeWindow(String opType) throws Exception {
	// final Map map = new HashMap();
	// map.put("opType", opType);
	// map.put("unitedDirectory", unitedDirectory);
	// Window win = (Window) Executions.createComponents(
	// "/pages/unitedDirectory/united_directory_tree_node_edit.zul",
	// this, map);
	// win.doModal();
	// win.addEventListener("onOK", new EventListener() {
	// @Override
	// public void onEvent(Event event) throws Exception {
	// Map dataMap = (Map) event.getData();
	// UnitedDirectory unitedDirectory = (UnitedDirectory) dataMap
	// .get("unitedDirectory");
	// if (map.get("opType").equals("addUnitedDirectoryRootNode")) {
	// onAddRootNodeResponse(unitedDirectory);
	// } else if (map.get("opType").equals(
	// "addUnitedDirectoryChildNode")) {
	// onAddChildNodeResponse(unitedDirectory);
	// }
	// }
	// });
	// }

	/**
	 * 删除节点
	 * 
	 * @throws Exception
	 */
	// public void onDelNode() throws Exception {
	// if (!PlatformUtil.checkPermissionDialog(getPortletInfoProvider(),
	// ActionKeys.DATA_OPERATING)) {
	// return;
	// }
	// if (this.unitedDirectoryTree.getSelectedItem() != null) {
	// Treechildren treechildren = this.unitedDirectoryTree
	// .getSelectedItem().getTreechildren();
	// if (treechildren != null) {
	// ZkUtil.showError("存在下级节点,不能删除", "提示信息");
	// return;
	// }
	// ZkUtil.showQuestion("你确定要删除该节点吗？", "提示信息", new EventListener() {
	// @Override
	// public void onEvent(Event event) throws Exception {
	// Integer result = (Integer) event.getData();
	// if (result == Messagebox.OK) {
	// organizationRelationManager
	// .removeOrganizationRelation(unitedDirectory);
	// Treechildren treechildren = (Treechildren) unitedDirectoryTree
	// .getSelectedItem().getParent();
	// treechildren.removeChild(unitedDirectoryTree
	// .getSelectedItem());
	// if (treechildren.getChildren().size() == 0) {
	// treechildren.getParent().removeChild(treechildren);
	// }
	// unitedDirectory = null;
	// /**
	// * 抛出删除节点成功事件
	// */
	// Events.postEvent(OrganizationConstant.ON_DEL_NODE_OK,
	// UnitedDirectoryTreeExt.this, null);
	// }
	// }
	// });
	// } else {
	// ZkUtil.showError("请选择你要删除的节点", "提示信息");
	// }
	// }

	/**
	 * 查询节点
	 * 
	 * @throws Exception
	 */
	// public void onSearchNode() throws Exception {
	// /**
	// * 数据权
	// */
	// if (!PlatformUtil.checkPermissionDialog(getPortletInfoProvider(),
	// ActionKeys.DATA_OPERATING)) {
	// return;
	// }
	// if (selectedTreeitem == null) {
	// ZkUtil.showError("请选择你要查询节点", "提示信息");
	// return;
	// }
	// Window win = (Window) Executions.createComponents(
	// "/pages/unitedDirectory/united_directory_tree_node_search.zul",
	// this, null);
	// win.setTitle("查找");
	// win.doOverlapped();
	// win.setLeft("30%");
	// win.setTop("30%");
	// win.addEventListener("onSearchOK", new EventListener() {
	// @Override
	// public void onEvent(Event event) throws Exception {
	// Map dataMap = (Map) event.getData();
	// String orgName = (String) dataMap.get("orgName");
	// searchNext(orgName);
	// }
	// });
	// }

	/**
	 * 点击时选择树
	 * 
	 * @throws Exception
	 */
	public void onSelect$unitedDirectoryTree() throws Exception {
		selectedTreeitem = this.unitedDirectoryTree.getSelectedItem();
		unitedDirectory = (UnitedDirectory) ((TreeNodeImpl) this.unitedDirectoryTree
				.getSelectedItem().getValue()).getEntity();
		this.setButtonValid(false, true, true);
		Events.postEvent(
				OrganizationConstant.ON_SELECT_UNITED_DIRECTORY_TREE_REQUEST,
				this, unitedDirectory);
	}

	/**
	 * 查找时选择树
	 * 
	 * @throws Exception
	 */
	// private void onSelectOrganizationRelationTree() {
	// unitedDirectory = (UnitedDirectory) ((TreeNodeImpl)
	// this.unitedDirectoryTree
	// .getSelectedItem().getValue()).getEntity();
	// if (unitedDirectory != null && unitedDirectory.getOrgId() != null) {
	// unitedDirectory = unitedDirectory.getOrganization();
	// }
	// Events.postEvent(
	// OrganizationConstant.ON_SELECT_COST_ORGANIZATION_TREE_REQUEST,
	// this, unitedDirectory);
	// }

	/**
	 * 查找时：选中查找的组织,并支持下一个
	 * 
	 * @param orgName
	 */
	// private void searchNext(String orgName) {
	// /**
	// * 选中要查询下级的节点
	// */
	// selectedTreeitem.setOpen(true);
	// Treechildren tc = selectedTreeitem.getTreechildren();
	// /**
	// * 当前选中的节点
	// */
	// Treeitem currentSelectTreeItem = unitedDirectoryTree.getSelectedItem();
	// if (tc != null) {
	// Collection<Treeitem> collection = tc.getItems();
	// if (collection != null && collection.size() > 0) {
	// int count = 0;
	// for (Treeitem ti : collection) {
	// if (ti != null) {
	// if (ti.getLabel().contains(orgName)) {
	// count++;
	// }
	// }
	// }
	// if (count == 0) {
	// ZkUtil.showInformation("该节点下没有该组织", "提示信息");
	// return;
	// } else if (count == 1) {
	// if (!selectedTreeitem.equals(currentSelectTreeItem)
	// && currentSelectTreeItem.getLabel().contains(
	// orgName)) {
	// ZkUtil.showInformation("该节点下没有其他符合条件的组织", "提示信息");
	// return;
	// }
	// for (Treeitem ti : collection) {
	// if (ti != null) {
	// if (ti.getLabel().contains(orgName)) {
	// ti.setSelected(true);
	// ti.focus();
	// onSelectOrganizationRelationTree();
	// return;
	// }
	// }
	// }
	// } else {
	// /**
	// * 多个组织符合条件：第一次查询
	// */
	// if (selectedTreeitem.equals(currentSelectTreeItem)) {
	// for (Treeitem ti : collection) {
	// if (ti != null) {
	// if (ti.getLabel().contains(orgName)) {
	// ti.setSelected(true);
	// ti.focus();
	// onSelectOrganizationRelationTree();
	// return;
	// }
	// }
	// }
	// } else {
	// /**
	// * 多个组织符合条件:继续往下查询
	// */
	// /**
	// * 用来判断是否到当前的匹配记录了
	// */
	// boolean flag = false;
	// /**
	// * 用来判断第几条
	// */
	// int falgCount = 0;
	//
	// for (Treeitem ti : collection) {
	// if (ti != null) {
	// if (flag) {
	// if (ti.getLabel().contains(orgName)) {
	// ti.setSelected(true);
	// ti.focus();
	// onSelectOrganizationRelationTree();
	// return;
	// } else {
	// continue;
	// }
	// }
	// /**
	// * 查询到当条了
	// */
	// if (ti.equals(currentSelectTreeItem)) {
	// flag = true;
	// if (ti.getLabel().contains(orgName)) {
	// falgCount++;
	// }
	// } else {
	// if (ti.getLabel().contains(orgName)) {
	// falgCount++;
	// }
	// }
	// /**
	// * 已经是最后一条了:从第一条查起
	// */
	// if (falgCount == count) {
	// for (Treeitem temp : collection) {
	// if (temp != null) {
	// if (temp.getLabel().contains(
	// orgName)) {
	// temp.setSelected(true);
	// ti.focus();
	// onSelectOrganizationRelationTree();
	// return;
	// }
	// }
	// }
	// }
	// }
	// }
	// }
	// }
	// } else {
	// ZkUtil.showInformation("该节点为末级节点,请重新选择你要查询的节点", "提示信息");
	// return;
	// }
	// }
	// }

	/**
	 * 设置按钮状态
	 * 
	 * @param canAddRoot
	 * @param canAddChild
	 * @param canDel
	 */
	private void setButtonValid(boolean canAddRoot, boolean canAddChild,
			boolean canDel) {
		this.addRootButton.setDisabled(!canAddRoot);
		this.addChildButton.setDisabled(!canAddChild);
		this.delChildButton.setDisabled(!canDel);
	}

	/**
	 * 供外层主动获取控件选择的组织
	 */
	public UnitedDirectory getSelectUnitedDirectory() {
		return unitedDirectory;
	}

	/**
	 * 判断名称是否要修改
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onSaveUnitedDirectoryInfoResponse(ForwardEvent event) {

		if (event.getOrigin().getData() != null) {
			UnitedDirectory unitedDirectory = (UnitedDirectory) event
					.getOrigin().getData();
			/**
			 * 修改名称
			 */
			this.unitedDirectoryTree.getSelectedItem().setLabel(
					unitedDirectory.getDeptname());
		}

	}

	/**
	 * 设置页面坐标
	 * 
	 * @param string
	 * @throws SystemException
	 * @throws Exception
	 */
	public void setPagePosition(String page) throws Exception {
		boolean canAddRoot = false;
		boolean canAddChild = false;
		boolean candelChild = false;

		if (PlatformUtil.isAdmin()) {
			canAddRoot = true;
			canAddChild = true;
			candelChild = true;
		} else if ("unitedDirectoryTreePage".equals(page)) {
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UNITED_DIRECTORY_TREE_ORG_ADD_ROOT)) {
				canAddRoot = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UNITED_DIRECTORY_TREE_ORG_ADD_CHILD)) {
				canAddChild = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(),
					ActionKeys.UNITED_DIRECTORY_TREE_ORG_DEL)) {
				candelChild = true;
			}
		}
		this.getAddRootButton().setVisible(canAddRoot);
		this.getAddChildButton().setVisible(canAddChild);
		this.getDelChildButton().setVisible(candelChild);
	}

}
